package com.sevenyear.excelanalyze.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sevenyear.excelanalyze.common.result.Result;
import com.sevenyear.excelanalyze.entity.EvaporationRate;
import com.sevenyear.excelanalyze.entity.HydroelectricConversionRate;
import com.sevenyear.excelanalyze.entity.param.queryConditionParam.RateQueryCondition;
import com.sevenyear.excelanalyze.service.HydroelectricConversionRateService;
import com.sevenyear.excelanalyze.service.PartitionBaseInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * <p>
 * 水电转换系数 前端控制器
 * </p>
 *
 * @author sevenyear
 * @since 2021-08-20
 */
@RestController
@CrossOrigin
@Api(value = "水电转换系数接口", tags = "06-水电转换系数相关接口")
@RequestMapping("/hydroelectricConversionRate")
public class HydroelectricConversionRateController {

    @Autowired
    PartitionBaseInfoService partitionBaseInfoService;
    @Autowired
    HydroelectricConversionRateService hydroelectricConversionRateService;

    @ApiOperation(value = "Excel批量导入")
    @PostMapping("/insert/excel")
    public Result insertRainfallInfiltrationRateByExcel(MultipartFile file) {
        hydroelectricConversionRateService.insertRainfallInfiltrationRateByExcel(file, hydroelectricConversionRateService, partitionBaseInfoService);
        return Result.ok();
    }


    @ApiOperation(value = "条件带分页查询")
    @PostMapping("/query/{current}/{limit}")
    public Result queryPageCondition(@PathVariable long current, @PathVariable long limit,
                                     @RequestBody(required = false) RateQueryCondition rateQueryCondition) {
        //创建page对象
        Page<HydroelectricConversionRate> page = new Page<>(current, limit);
        hydroelectricConversionRateService.queryPageByCondition(page, rateQueryCondition);
        long total = page.getTotal();
        List<HydroelectricConversionRate> records = page.getRecords();
        return Result.ok().data("total", total).data("list", records);
    }

    @ApiOperation(value = "手动添加单条记录")
    @PostMapping("/insert")
    public Result insert(@RequestBody(required = true) HydroelectricConversionRate hydroelectricConversionRate) {
        boolean flag = hydroelectricConversionRateService.saveOrUpdate(hydroelectricConversionRate);
        if (flag) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }

    @ApiOperation(value = "根据主键id删除信息")
    @DeleteMapping("/delete/{id}")
    public Result deleteById(@PathVariable Long id) {
        boolean flag = hydroelectricConversionRateService.removeById(id);
        if (flag) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }

    @ApiOperation(value = "查询所有信息，供excel导出使用")
    @GetMapping("/query/allList")
    public Result queryAllPartitionBaseInfo() {
        List<HydroelectricConversionRate> allList = hydroelectricConversionRateService.list(null);
        return Result.ok().data("allList", allList);
    }
}

